home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / SWar / Game.p < prev    next >
Text File  |  1995-06-15  |  6KB  |  225 lines

  1. unit Game;
  2.  
  3. interface
  4.     uses
  5. {$IFC UNDEFINED THINK_PASCAL}
  6.         Types, QuickDraw, Events, Menus, Dialogs, Fonts, Resources, Devices,
  7. {$ENDC}
  8.         Globals, Util, Ships, Shots, Debris, Stars, SWSound;
  9.  
  10.     procedure InitGame;
  11.     procedure GameCycle;
  12.  
  13. implementation
  14.  
  15.  
  16.     procedure MoveObjects;
  17.     begin
  18.         MoveShips;
  19.         MoveShots;
  20.         MoveDebris;
  21.     end; (* MoveObjects() *)
  22.  
  23.     procedure DrawObjects;
  24.     begin
  25.         DrawStars;
  26.         DrawShips;
  27.         DrawShots;
  28.         DrawDebris;
  29.     end; (* DrawObjects() *)
  30.  
  31.     procedure NewMatch;
  32. {     evar Handle        gNewMatchSoundH:xtern;}
  33. {    eBoolean        gMatchIsEnding:xtern; }
  34.     begin
  35.         AStopSnd;
  36.         AInitSnd;
  37.         InitStars;
  38.         InitShips;
  39.         InitShots;
  40.         InitDebris;
  41.         if noErr <> ASndPlay(gNewMatchSoundH) then
  42.             ;
  43.         gMatchIsEnding := FALSE;
  44.     end; (* NewMatch() *)
  45.  
  46.     var
  47.         optionStillPressed: Boolean;
  48.         barStillPressed: Integer;
  49.         CWStillPressed: Integer;
  50.         CCWStillPressed: Integer;
  51.         thStillPressed: Integer;
  52.  
  53.     procedure CheckKeyboard;
  54. {Note: Those codes are the codes on the author's keyboard - not necessarily the same on}
  55. {all other keyboards! They should rather be configurable.}
  56.         const
  57.             COMMAND_KEY = $37;(* 'CLOVER' *)
  58.             SHIFT_KEY = $38;(* 'SHIFT' *)
  59.             OPTION_KEY = $3A;(* 'OPTION' *)
  60.             CONTROL_KEY = $3B;(* 'CONTROL' *)
  61.             ESCAPE_KEY = $35;(* 'ESC' *)
  62.             QUIT_KEY = $0C;(* 'Q' *)
  63.             NEW_KEY = $2D;(* 'N' *)
  64.             SPEEDUP_KEY = $18;(* '+' *)
  65.             SLOWDOWN_KEY = $1B;(* '-' *)
  66.             PAUSE_KEY = $23;(* 'P' *)
  67.             INFO_KEY = $22;(* 'I' *)
  68.             SOUND_KEY = $01;(* 'S' *)
  69.             SHOT_KEY = $3B;(* 'CONTROL' *)
  70.             UP_ARROW = $7E;(* 'UP ARROW' *)
  71.             DOWN_ARROW = $7D;(* 'DOWN ARROW' *)
  72.             LEFT_ARROW = $7B;(* 'LEFT ARROW' *)
  73.             RIGHT_ARROW = $7C;(* 'RIGHT ARROW' *)
  74.             THRUST_KEY = $7E;(* 'UP ARROW' *)
  75.             DOWN_KEY = $28;(* 'K *)
  76.             CW_KEY = $7C;(* 'RIGHT ARROW' *)
  77.             CCW_KEY = $7B;(* 'LEFT ARROW' *)
  78.             SPACE_BAR = $31;(* ' ' *)
  79.             F1_KEY = $7A;(* F1 *)
  80.             F2_KEY = $78;(* F2 *)
  81.             F3_KEY = $63;(* F3 *)
  82.             F4_KEY = $76;(* F4 *)
  83.         const
  84.             SHOT_SPEED = 7;
  85.     begin
  86.         if (gShipRecs[0].isAlive and IsPressed(SHOT_KEY)) then
  87.             begin
  88.                 if barStillPressed = 0 then
  89.                     begin
  90.                         barStillPressed := 3;
  91.                         case (gShipRecs[0].dir) of
  92.                             0: 
  93.                                 CreateShot(gShipRecs[0].color, gShipRecs[0].where.h + 7, gShipRecs[0].where.v - 2, 0, gShipRecs[0].vel.v - SHOT_SPEED);
  94.                             1: 
  95.                                 CreateShot(gShipRecs[0].color, gShipRecs[0].where.h + 17, gShipRecs[0].where.v - 2, gShipRecs[0].vel.h + SHOT_SPEED, gShipRecs[0].vel.v - SHOT_SPEED);
  96.                             2: 
  97.                                 CreateShot(gShipRecs[0].color, gShipRecs[0].where.h + 17, gShipRecs[0].where.v + 7, gShipRecs[0].vel.h + SHOT_SPEED, 0);
  98.                             3: 
  99.                                 CreateShot(gShipRecs[0].color, gShipRecs[0].where.h + 17, gShipRecs[0].where.v + 17, gShipRecs[0].vel.h + SHOT_SPEED, gShipRecs[0].vel.v + SHOT_SPEED);
  100.                             4: 
  101.                                 CreateShot(gShipRecs[0].color, gShipRecs[0].where.h + 7, gShipRecs[0].where.v + 17, 0, gShipRecs[0].vel.v + SHOT_SPEED);
  102.                             5: 
  103.                                 CreateShot(gShipRecs[0].color, gShipRecs[0].where.h - 2, gShipRecs[0].where.v + 17, gShipRecs[0].vel.h - SHOT_SPEED, gShipRecs[0].vel.v + SHOT_SPEED);
  104.                             6: 
  105.                                 CreateShot(gShipRecs[0].color, gShipRecs[0].where.h - 2, gShipRecs[0].where.v + 7, gShipRecs[0].vel.h - SHOT_SPEED, 0);
  106.                             7: 
  107.                                 CreateShot(gShipRecs[0].color, gShipRecs[0].where.h - 2, gShipRecs[0].where.v - 2, gShipRecs[0].vel.h - SHOT_SPEED, gShipRecs[0].vel.v - SHOT_SPEED);
  108.                         end; (* case *)
  109.                         if noErr <> ASndPlay(gShotSoundH) then
  110.                             ;
  111.                     end; (* if *)
  112.             end; (* if *)
  113.  
  114.         if barStillPressed <> 0 then
  115.             barStillPressed := barStillPressed - 1;
  116.  
  117.         gShipRecs[0].isAccel := FALSE;
  118.  
  119.         if (gShipRecs[0].isAlive and IsPressed(THRUST_KEY)) then
  120.             begin
  121.                 if (thStillPressed = 0) then
  122.                     begin
  123.                         thStillPressed := 5;
  124.                         gShipRecs[0].isAccel := TRUE;
  125.                     end; (* if *)
  126.             end; (* if *)
  127.  
  128.         if (thStillPressed <> 0) then
  129.             thStillPressed := thStillPressed - 1;
  130.  
  131.  
  132.         if (gShipRecs[0].isAlive and IsPressed(CW_KEY)) then
  133.             begin
  134.                 if (CWStillPressed = 0) then
  135.                     begin
  136.                         CWStillPressed := 5;
  137.                         gShipRecs[0].dir := gShipRecs[0].dir + 1;
  138.                     end; (* if *)
  139.             end; (* if *)
  140.  
  141.         if (CWStillPressed <> 0) then
  142.             CWStillPressed := CWStillPressed - 1;
  143.  
  144.         if (gShipRecs[0].isAlive and IsPressed(CCW_KEY)) then
  145.             begin
  146.                 if CCWStillPressed = 0 then
  147.                     begin
  148.                         CCWStillPressed := 5;
  149.                         gShipRecs[0].dir := gShipRecs[0].dir - 1;
  150.                     end; (* if *)
  151.             end; (* if *)
  152.  
  153.         if CCWStillPressed <> 0 then
  154.             CCWStillPressed := CCWStillPressed - 1;
  155.  
  156.         if gShipRecs[0].dir < 0 then
  157.             gShipRecs[0].dir := 7;
  158.         if gShipRecs[0].dir > 7 then
  159.             gShipRecs[0].dir := 0;
  160.  
  161.         if (IsPressed(COMMAND_KEY) and IsPressed(NEW_KEY)) then
  162.             begin
  163.                 InitGame;
  164.                 FlushEvents(everyEvent, 0);
  165.             end; (* if *)
  166.  
  167. {    if (IsPressed(COMMAND_KEY) && IsPressed(PAUSE_KEY)) {}
  168. {        while (!Button());}
  169. {        FlushEvents(everyEvent, 0);}
  170.  
  171. {    if (IsPressed(COMMAND_KEY) && IsPressed(SOUND_KEY)) {}
  172. {        gSoundOn:= not gSoundOn;}
  173. {        SetChecks();}
  174. {        FlushEvents(everyEvent, 0);}
  175.  
  176.     end; (* CheckKeyboard() *)
  177.  
  178.     procedure InitGame;
  179.     begin
  180.         NewMatch;
  181.     end; (* InitGame() *)
  182.  
  183.     procedure GameCycle;
  184.         const
  185.             kMinFrameTicks = 2; {max 30 fps}
  186.         var
  187.             savePort: GrafPtr;
  188.             bigRect: Rect;
  189.             startTicks: Longint;
  190.     begin
  191.         startTicks := TickCount;
  192.  
  193.         bigRect.top := 0;
  194.         bigRect.bottom := 479;
  195.         bigRect.left := 0;
  196.         bigRect.right := 639;
  197.         GetPort(savePort);
  198.         SetPort(GrafPtr(gPictureWindow));
  199.         CheckKeyboard;
  200.         MoveObjects;
  201.         if (not gPlaying) then
  202.             exit(GameCycle);
  203.         DrawObjects;
  204.         SetPort(savePort);
  205.  
  206.         gMatchTicker := gMatchTicker - 1;
  207.         if (gMatchIsEnding and (gMatchTicker = 0)) then
  208.             begin
  209.                 if gOldDepth > 1 then
  210.                     begin
  211.                         RGBForeColor(myBlack);
  212.                         RGBBackColor(myBlack);
  213.                     end;
  214.                 EraseRect(bigRect);
  215.                 RGBForeColor(myBlack);
  216.                 RGBBackColor(myWhite);
  217.                 NewMatch;
  218.             end; (* if *)
  219.  
  220.         while TickCount < startTicks + kMinFrameTicks do
  221.             ;
  222.  
  223.     end; (* GameCycle() *)
  224.  
  225. end.